package com.foreknow.elm.service.Impl;

import com.foreknow.elm.dao.DeliveryAddressDao;
import com.foreknow.elm.dao.Impl.DeliveryAddressDaoImpl;
import com.foreknow.elm.po.DeliveryAddress;
import com.foreknow.elm.service.DeliveryAddressService;
import com.foreknow.elm.util.DBUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DeliveryAddressServiceImpl implements DeliveryAddressService {
    @Override
    public List<DeliveryAddress> listDeliveryAddressByUserId(String userId){
        List<DeliveryAddress> list=new ArrayList<>();
        DeliveryAddressDao dad=new DeliveryAddressDaoImpl();
        try {
            list=dad.listDeliveryAddressByUserId(userId);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return list;
    }

    @Override
    public DeliveryAddress getDeliveryAddressById(Integer daId){
        DeliveryAddress da=new DeliveryAddress();
        DeliveryAddressDao dad=new DeliveryAddressDaoImpl();
        try {
            da=dad.getDeliveryAddressById(daId);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return da;
    }

    @Override
    public int saveDeliveryAddress(String contactName, Integer contactSex, String contactTel, String address, String userId) {
        int i=0;
        DeliveryAddressDao dad=new DeliveryAddressDaoImpl();
        try {
            DBUtil.beginTransaction("false");
            i=dad.saveDeliveryAddress(contactName,contactSex,contactTel,address,userId);
            DBUtil.commitTransaction();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (Exception e) {
            try {
                DBUtil.rollbackTransaction();
            }catch (Exception e1){
                e1.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return i;
    }

    @Override
    public int updateDeliveryAddress(Integer daId, String contactName, Integer contactSex, String contactTel, String address, String userId) {
        int i=0;
        DeliveryAddressDao dad=new DeliveryAddressDaoImpl();
        try {
            DBUtil.beginTransaction("false");
            i=dad.updateDeliveryAddress(daId,contactName,contactSex,contactTel,address,userId);
            DBUtil.commitTransaction();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (Exception e) {
            try {
                DBUtil.rollbackTransaction();
            }catch (Exception e1){
                e1.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return i;
    }

    @Override
    public int removeDeliveryAddress(Integer daId){
        int i=0;
        DeliveryAddressDao dad=new DeliveryAddressDaoImpl();
        try {
            DBUtil.beginTransaction("false");
            i=dad.removeDeliveryAddress(daId);
            DBUtil.commitTransaction();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (Exception e) {
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return i;
    }

    public static void main(String[] args) {
        DeliveryAddressService das=new DeliveryAddressServiceImpl();

//        System.out.println(das.removeDeliveryAddress(18));

//        System.out.println(das.updateDeliveryAddress(18,"ABC12",3,"Japan","",""));

//        System.out.println(das.saveDeliveryAddress("tom",1,"12314","anshan","12344321"));

//        List<DeliveryAddress>list= das.listDeliveryAddressByUserId("11111111111");
//        for (int i=0;i< list.size();i++){
//            System.out.println(list.get(i).getContactName());
//            }

//        DeliveryAddress da=das.getDeliveryAddressById(2);
//        System.out.println(da.getContactName());
    }
    }

